import React from 'react'
import {Divider, Form, Icon} from 'antd'
import {connect} from 'react-redux'
import UserSearchBox from './user-search-box'
import {
  addOrganizationAdminRole,
  getOrganization,
  getOrganizationUsers,
  removeAdminRole
} from "../../actions/organization";
import {searchUserByUsernameOrEmail} from "../../actions/user";
import AdministratorList from "./administrator-list";

class OrganizationAdminManagement extends React.Component {
  state = {}

  componentDidMount = () => {
    const {organizationId} = this.props.match.params
    this.props.getOrganization(organizationId)
    this.props.getOrganizationUsers(organizationId)
  }

  removeAdminRole = userId => {
    const {organization} = this.props
    this.props.removeAdminRole(organization.id, userId)
  }

  render() {
    const {organization, users} = this.props
    const {organizationUsers} = users

    return <div>
      <a onClick={this.props.history.goBack}>
        <Icon type="arrow-left"/> 返回
      </a>
      <Divider orientation='left'>
        {organization.title} 组织
      </Divider>

      <UserSearchBox
        organization={organization}
        users={users}
        addOrganizationAdminRole={this.props.addOrganizationAdminRole }
        searchUserByUsernameOrEmail={this.props.searchUserByUsernameOrEmail}
      />
      <Divider orientation='left'>
        管理员
      </Divider>
      <AdministratorList
        organizationUsers={organizationUsers}
        removeRole={this.removeAdminRole}
      />
    </div>
  }
}

const DistributeAdminModalForm = Form.create()(OrganizationAdminManagement)

const mapStateToProps = ({organization, users}) => ({
  organization,
  users
})
const mapDispatchToProps = dispatch => ({
  getOrganization: organizationId => dispatch(getOrganization(organizationId)),
  searchUserByUsernameOrEmail: usernameOrEmail => dispatch(searchUserByUsernameOrEmail(usernameOrEmail)),
  getOrganizationUsers: organizationId => dispatch(getOrganizationUsers(organizationId)),
  addOrganizationAdminRole: (organizationId,userId) => dispatch(addOrganizationAdminRole(organizationId, userId)),
  removeAdminRole: (organizationId, userId) => dispatch(removeAdminRole(organizationId,userId))
})
export default connect(mapStateToProps, mapDispatchToProps)(DistributeAdminModalForm)
